package com.insurance.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.insurance.bean.Product;
import com.insurance.service.ProductService;
import com.insurance.util.PageBean;

@Transactional
public class ProductServiceBean implements ProductService {
	@Resource private SessionFactory sessionFactory;

	public void save(Product product){
		sessionFactory.getCurrentSession().persist(product);
	}
	public void update(Product product){
		sessionFactory.getCurrentSession().merge(product);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Product getProduct(Integer productid){
		return (Product)sessionFactory.getCurrentSession().get(Product.class, productid);
	}

	public void delete(Integer productid){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Product.class, productid));
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Product> getProducts(){		
		return sessionFactory.getCurrentSession().createQuery("from Product").list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Product> getProductsTop10(){		
		return sessionFactory.getCurrentSession().createQuery("from Product order by id desc").setFirstResult(0).
		setMaxResults(10).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Product> getProductsTop5(){		
		return sessionFactory.getCurrentSession().createQuery("from Product order by id desc").setFirstResult(0).
		setMaxResults(5).list();
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)	
	@SuppressWarnings("unchecked")
	public List<Product> getProductsByPage(PageBean page){
		
		int first = (page.getCurrentpage() - 1) * page.getPagesize();
		return sessionFactory.getCurrentSession().createQuery("from Product p order by p.id desc").
		setFirstResult(first).setMaxResults(page.getPagesize()).list();
		
	}
	
	public int getTotalCount() {
		
		return ((Long)sessionFactory.getCurrentSession().createQuery
				("select count(*) from Product").uniqueResult()).intValue();
		
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Product> getProductsByType(Integer type){		
		
		return sessionFactory.getCurrentSession().createQuery
		("from Product p where p.type=" + type).setMaxResults(10).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Product> getProductsByCompany(String companyName){
		
		return sessionFactory.getCurrentSession().createQuery
		("from Product p where p.companyName=" + "companyName").list();
		
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Product> searchProduct(Product product){
		String hql = "from Product p where p.type = ? and p.companyName = ?";
		return sessionFactory.getCurrentSession().createQuery(hql).
		setParameter(0, product.getType()).setParameter(1,product.getCompanyName()).list();
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Product> getProductsByAgent(Integer agentId){		
		
		return sessionFactory.getCurrentSession().createQuery
		("from Product p, Proxy r, Agent a where p.id=r.productId and r.agentId=a.id and a.id=" + agentId).list();
	}
}
